package com.yiren.dbaa.module.exam.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.PointCode;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.util.DensityUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2022/2/28
 * Describe 项目信息列表适配器
 */
public class DjProjectInfoAdapter extends BaseAdapter {

    private Context context;
    private List<ProjectEntity> projects;
    private HashMap<String, Integer> projectCodeMap;
    private OnProjectClickListener listener;

    public DjProjectInfoAdapter(Context context, List<ProjectEntity> projects, HashMap<String, Integer> projectCodeMap, OnProjectClickListener listener) {
        this.context = context;
        this.projects = projects;
        this.projectCodeMap = projectCodeMap;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return projects.size();
    }

    @Override
    public Object getItem(int position) {
        return projects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dj_project_info, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProjectEntity project = projects.get(position);
        if (null != project) {
            holder.btProject.setText(project.getProjectType().equals(ProjectTypeEnum.LIGHT.toString()) ? "灯光练习" : project.getName());
            if (project.getId().equals(PointCode.LKYZ_CODE)) {
                holder.btProject.setTag(StringConstant.EXAM_KBTC);
            }
        }
        convertView.setLayoutParams(new GridView.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, (parent.getHeight() - DensityUtil.dip2px(context, context.getResources().getDimension(R.dimen.dp_9))) / 3));
        if (project.getProjectType().equals(ProjectTypeEnum.LIGHT.toString())) {
            holder.btProject.setBackgroundResource(R.drawable.exam_point_light_shape);
        } else {
            TaskStateEnum taskState = project.getTaskState();
            if (null != taskState) {
                if (taskState.equals(TaskStateEnum.IN_PROCESS)) {
                    holder.btProject.setBackgroundResource(R.drawable.exam_point_doing_shape);
                } else if (taskState.equals(TaskStateEnum.FINISH)) {
                    holder.btProject.setBackgroundResource(R.drawable.exam_point_complete_shape);
                }
            } else {
                holder.btProject.setBackgroundResource(R.drawable.exam_point_normal_shape);
            }
        }
        if (projectCodeMap.containsKey(project.getId())) {
            if (projectCodeMap.get(project.getId()) > 0) {  // 扣分
                holder.btProject.setTextColor(Color.parseColor("#ff4850"));
            } else {  // 未扣分
                holder.btProject.setTextColor(Color.WHITE);
            }
        } else {
            holder.btProject.setTextColor(Color.WHITE);
        }

        holder.btProject.setOnClickListener(v -> {
            listener.onProjectClick(position);
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.bt_project)
        Button btProject;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 项目点击事件回调
     */
    public interface OnProjectClickListener {

        void onProjectClick(int position);
    }

}
